package com.relife.sys.service;

import com.relife.core.entity.ProcessConfigEntity;
import com.relife.core.entity.ProcessConfigListEntity;
import com.relife.core.entity.ProcessSetEntity;
import com.relife.core.service.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service("pConfigService")
@Transactional
public class ProcessConfigService extends BaseService {

	/**
	 * 保存
	 * 
	 * @author WU
	 * @date 2016年5月24日 上午10:01:03
	 * @param pConfig
	 */
	public void saveProcessConfig(ProcessConfigEntity pConfig) {
		this.saveOrUpdate(pConfig);

		if (pConfig.getpConfigList() == null || pConfig.getpConfigList().size() == 0) {
			throw new RuntimeException("审批明细不能为空！");
		}

		this.deleteList(getList(ProcessConfigListEntity.class, "processConfig.id", pConfig.getId()));

		ProcessSetEntity pSet = this.get(ProcessSetEntity.class, pConfig.getProcessSet().getId());

		for (int i = 0; i < pConfig.getpConfigList().size(); i++) {
			if (pConfig.getpConfigList().get(i).getNodeKey() != null) {
				pConfig.getpConfigList().get(i).setProcessConfig(pConfig);
				pConfig.getpConfigList().get(i).setProcessKey(pSet.getProcesskey());
				pConfig.getpConfigList().get(i).setDepartId(pConfig.getDepart().getId());

				if (pConfig.getpConfigList().get(i).getUser() != null && pConfig.getpConfigList().get(i).getUser().getId() == null) {
					pConfig.getpConfigList().get(i).setUser(null);
				}

				if (pConfig.getpConfigList().get(i).getRole() != null && pConfig.getpConfigList().get(i).getRole().getId() == null) {
					pConfig.getpConfigList().get(i).setRole(null);
				}
				this.save(pConfig.getpConfigList().get(i));
			}
		}

	}
}
